<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    .center {
      margin: 0 auto;
    }

    .outer {
      width: 300px;
      height: 300px;
      border: 1px solid #ccc;
      background-color: skyblue;
      display: flex;
    }

    .inner {
      width: 150px;
      height: 150px;
      border: 1px solid #ccc;
      background-color: pink;
      margin: auto;
    }
  </style>
</head>

<body>
  <div id="root">
    <div class="outer center">
      <div class="inner"></div>
    </div>
  </div>

  <script>
    let root = document.getElementById('root'),
      inner = document.querySelector('.inner'),
      outer = document.querySelector('.outer')

    console.log('rrot::: ', root)

    // 在组件渲染阶段，会给元素属性添加 onXxx/onXxxCapture 属性
    outer.onClick = () => { console.log('outer 冒泡') }
    outer.onClickCapture = () => { console.log('outer 捕获') }
    inner.onClick = () => { console.log('inner 冒泡') }
    inner.onClickCapture = () => { console.log('inner 捕获') }

    // 合成事件捕获
    root.addEventListener('click', (ev) => {
      let path = ev.path || ev.composedPath();
      [...path].reverse().forEach(ele => {
        let handle = ele.onClickCapture
        if (handle) {
          handle()
        }
      })
    }, true)
    // 合成事件捕冒泡
    root.addEventListener('click', (ev) => {
      let path = ev.path || ev.composedPath();
      [...path].forEach(ele => {
        let handle = ele.onClick
        if (handle) {
          handle()
        }
      })
    }, true)
  </script>
</body>

</html>